package com.distributeredis.redis_springboot.springapachehttpclienthttpconnectionresttemplate.httpclient.study2.httpclientcontroller;

import cn.hutool.json.JSONObject;
import com.distributeredis.redis_springboot.springapachehttpclienthttpconnectionresttemplate.httpclient.study2.vo.BasicEntity;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URI;
import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/httpclient")
public class TestDoGetController {

    @GetMapping("/clientGet")
    public String doGet(BasicEntity basicEntity) throws Exception {
        // 创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建URI对象，并且设置请求参数
        URI uri = new URIBuilder("http://localhost:8080/test/doget")
                .setParameter("name", basicEntity.getName())
                .setParameter("sex",basicEntity.getSex())
                .build();
        // 创建请求对象
        HttpGet httpGet = new HttpGet(uri);

        // 发送请求，接收响应结果
        CloseableHttpResponse response = httpClient.execute(httpGet);
        // 获取服务器返回的状态码
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("服务器返回的状态码："+statusCode);

        HttpEntity entity = response.getEntity();
        String body = EntityUtils.toString(entity);
        System.out.println("服务器返回的数据："+body);

        // 关闭资源
        response.close();
        httpClient.close();
        return "服务器返回的数据为："+body;
    }

    @GetMapping("/clientGet1")
    public String doGet1() throws Exception {
        // 创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建URI对象，并且设置请求参数
        URI uri = new URIBuilder("http://localhost:8080/test/doget1")
                .build();
        // 创建请求对象
        HttpGet httpGet = new HttpGet(uri);

        // 发送请求，接收响应结果
        CloseableHttpResponse response = httpClient.execute(httpGet);
        // 获取服务器返回的状态码
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("服务器返回的状态码："+statusCode);

        HttpEntity entity = response.getEntity();
        String body = EntityUtils.toString(entity);
        System.out.println("服务器返回的数据："+body);

        // 关闭资源
        response.close();
        httpClient.close();
        return "服务器返回的数据为："+body;
    }

    @PostMapping("/doPost")
    public String testDoPost() throws Exception {
        //创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建URI对象，并且设置请求参数
        URI uri = new URIBuilder("http://localhost:8080/test/dopost")
                .build();
        HttpPost httpPost = new HttpPost(uri);

        JSONObject jsonObject = new JSONObject();
        jsonObject.putOpt("name","张三");
        jsonObject.putOpt("sex","男");

        StringEntity entity = new StringEntity(jsonObject.toString(), StandardCharsets.UTF_8);
        // 指定请求编码方式
        entity.setContentEncoding("utf-8");
        // 数据格式
        entity.setContentType("application/json");
        httpPost.setEntity(entity);

        // 发送请求
        CloseableHttpResponse response = httpClient.execute(httpPost);
        //解析返回结果
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("响应码为：" + statusCode);

        HttpEntity entity1 = response.getEntity();
        String body = EntityUtils.toString(entity1);
        System.out.println("响应数据为：" + body);

        //关闭资源
        response.close();
        httpClient.close();
        return  body;
    }

}
